package com.jierui.meter.frontend.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.function.Function;

public class MyPageImpl<T> extends PageImpl<T> {
    private static final long serialVersionUID = 867755909294344406L;
    private final long total;

    /**
     * total 直接赋值为count查询出来的结果，不再根据offset和pageSize计算
     * @param content
     * @param pageable
     * @param total
     */
    public MyPageImpl(List<T> content, Pageable pageable, long total) {
        super(content, pageable, 0);
        this.total = total;
    }

    public MyPageImpl(List<T> content) {
        this(content, Pageable.unpaged(), null == content ? 0L : (long)content.size());
    }

    @Override
    public int getTotalPages() {
        return this.getSize() == 0 ? 1 : (int)Math.ceil((double)this.total / (double)this.getSize());
    }

    @Override
    public long getTotalElements() {
        return this.total;
    }

    @Override
    public <U> Page<U> map(Function<? super T, ? extends U> converter) {
        return new MyPageImpl(this.getConvertedContent(converter), this.getPageable(), this.total);
    }
}
